var MaterialStat = function () {
  var SearchForm = function () {
    var template = /* html */ `
      <div class="search-form" v-loading.fullscreen.lock="loading">
        <sys-common-search request-url="materialSelectFilter/getMaterialSelectData" :request-data="{type:26}"
          :search="search" :search-field="searchField" label-width="auto" input-width="180px">
          <template slot="prepend">
            <el-form-item label="部门-车间">
              <el-cascader filterable clearable v-model="search.deptWork" style="width:180px" :options="deptWorkList"
                :props="props" separator="-"></el-cascader>
            </el-form-item>
          </template>
        </sys-common-search>
      </div>
    `;

    return {
      props: {
        search: {
          type: Object,
          required: true
        }
      },

      data: function () {
        return {
          loading: false,
          searchField: ['materialName', 'model', 'color', 'providerName'],
          deptWorkList: [],
          props: {
            value: 'id',
            label: 'name'
          }
        };
      },

      created: function () {
        this.setDeptList();
      },

      methods: {
        setDeptList: function () {
          this.loading = true;
          var self = this;
          ajaxRequest('equipment/getDepartmentDropdown').then(function (deptWorkList) {
            deptWorkList = _.map(deptWorkList, function (deptWork) {
              var obj = {
                id: deptWork.departmentCode,
                name: deptWork.deptName
              };

              var workshops = deptWork.workshops;
              workshops = _.map(workshops, function (workshop) {
                return {
                  id: workshop.id,
                  name: workshop.workshopName
                };
              });
              if (!SysUtil.isEmpty(workshops)) {
                obj.children = workshops;
              }
              return obj;
            });

            self.deptWorkList = deptWorkList;
            self.loading = false;
          }).catch(function () {
            self.loading = false;
          });
        }
      },

      components: {
        SysCommonSearch: SysCommonSearch
      },

      template: template
    };
  }();

  var template = /* html */ `
    <div class="material-stat" v-loading.fullscreen.lock="loading">
      <search-btn-view btn-text="查询" @before-open="resetSearch" @search="searchData">
        <template slot="append">
          <template v-if="selectStatList.length>0">
            <el-button size="medium" type="primary" @click="retreatStatList(false)">批量退回</el-button>
            <el-button size="medium" type="primary" @click="retreatStatList(true)">退回审核过账</el-button>
          </template>
        </template>
        <div slot="search-content">
          <search-form :search="search"></search-form>
        </div>
      </search-btn-view>
      <div class="material-stat-table-div">
        <el-table fit stripe border :data="statList" style="width:100%;" height="parent" @selection-change="selectionChange">
          <el-table-column type="selection" width="40px" align="center" fixed="left"></el-table-column>
          <el-table-column sortable label="车间" prop="workShopName" min-width="100px" align="center"></el-table-column>
          <el-table-column sortable label="供应商" prop="providerName" min-width="120px" header-align="center"></el-table-column>
          <el-table-column sortable label="物料名称" prop="materialName" min-width="140px" header-align="center"></el-table-column>
          <el-table-column sortable label="规格" prop="model" min-width="120px" header-align="center"></el-table-column>
          <el-table-column sortable label="编号" prop="color" min-width="120px" header-align="center"></el-table-column>
          <el-table-column sortable label="供方编号" prop="providerNo" min-width="120px" header-align="center"></el-table-column>
          <el-table-column sortable label="总发料数" prop="sumCount" min-width="110px" align="center"></el-table-column>
          <el-table-column sortable label="车间已用数" prop="useWorkShopCount" min-width="110px" align="center"></el-table-column>
          <el-table-column sortable label="实际库存数" prop="actualCount" min-width="110px" align="center"></el-table-column>
          <el-table-column sortable label="车间占用数" prop="useFactoryCount" min-width="110px" align="center"></el-table-column>
          <el-table-column sortable label="可用数" prop="stockCount" min-width="110px" align="center"></el-table-column>
          <el-table-column :resizable="false" label="退回数量" min-width="150px" header-align="center">
            <template slot-scope="scope">
              <el-input-number is-decimal-zero v-model="scope.row.count" style="width:140px" size="mini" :controls="false" :max="scope.row.maxCount"
                :min="0" :precision="4"></el-input-number>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="备注" min-width="190px" header-align="center">
            <template slot-scope="scope">
              <el-input v-model="scope.row.remark" size="mini" style="width:180px"></el-input>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <table-pagination v-if="pagination.totalCount!==0" :pagination="pagination" @change="pageChange">
      </table-pagination>
    </div>
  `;

  return {
    mixins: [
      tablePaginationMixin()
    ],

    data: function () {
      var search = {
        materialName: '',
        model: '',
        color: '',
        providerName: '',
        deptWork: []
      };
      return {
        loading: false,
        statList: [],
        search: _.cloneDeep(search),
        oldSearch: _.cloneDeep(search),
        selectStatList: []
      };
    },

    created: function () {
      this.setStatList(true);
    },

    methods: {
      setStatList: function (isSearch) {
        if (!isSearch) {
          this.search = _.cloneDeep(this.oldSearch);
        }

        var search = this.search;
        var pagination = this.pagination;
        var currentPage = pagination.currentPage;
        var pageSize = pagination.pageSize;
        var start = (currentPage - 1) * pageSize;
        var postData = {
          start: start,
          length: pageSize
        }

        var deptWork = search.deptWork;
        var workShopId = deptWork[1];
        if (!SysUtil.isEmpty(workShopId)) {
          postData.workShopId = workShopId;
        }

        var fields = ['materialName', 'model', 'color', 'providerName'];
        SysUtil.setPostData(postData, search, fields, false, true);

        this.loading = true;
        var self = this;
        ajaxRequest('issueNote/getWorkShopMaterialCountPage', postData).then(function (res) {
          if (isSearch) {
            self.oldSearch = _.cloneDeep(self.search);
          }

          var statList = res.returnData;
          for (var i = 0; i < statList.length; i++) {
            var stat = statList[i];
            stat.model = stat.modal;
            stat.count = undefined;
            stat.remark = '';

            var stockCount = stat.stockCount;
            if (stockCount === null) {
              stockCount = undefined;
            }
            stat.maxCount = stockCount;
          }

          self.statList = statList;
          self.pagination.totalCount = res.iTotalRecords;
          self.loading = false;
        }).catch(function () {
          self.loading = false;
        });
      },

      resetSearch: function () {
        this.search = _.cloneDeep(this.oldSearch);
      },

      searchData: function () {
        this.pagination.currentPage = 1;
        this.setStatList(true);
      },

      pageChange: function () {
        this.setStatList();
      },

      selectionChange: function (selectStatList) {
        this.selectStatList = selectStatList;
      },

      retreatStatList: function (isAduit) {
        var statList = this.selectStatList;
        if (statList.length <= 0) {
          return;
        }

        var postDataList = [];
        var fields = ['materialCode', 'materialName', 'color', 'providerName', 'model', 'workShopId', 'count', 'remark'];
        for (var i = 0; i < statList.length; i++) {
          var stat = statList[i];
          var count = stat.count;
          if (SysUtil.isEmpty(count)) {
            this.$alert('请输入退回数量', '信息');
            return;
          }

          if (count === 0) {
            this.$alert('退回数量不能为0', '信息');
            return;
          }

          var obj = {
            warehouseType: 4
          };
          SysUtil.setPostData(obj, stat, fields);
          postDataList.push(obj);
        }

        this.loading = true;
        var self = this;
        ajaxRequest('warehouse/noOrderReturnToWarehouse', {
          array: JSON.stringify(postDataList),
          isAduit: isAduit
        }).then(function () {
          self.loading = false;
          self.$alert('成功批量退回', '信息', {
            callback: function () {
              self.setStatList();
            }
          });
        }).catch(function () {
          self.loading = false;
        });
      }
    },

    components: {
      SearchBtnView: SearchBtnView,
      SearchForm: SearchForm
    },

    template: template
  };
}();
